//
//  TopImageCell.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/10.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SlideTopImageCell.h"

#import "CategoryViewController.h"

#import <SDWebImage/UIButton+WebCache.h>

@interface SlideTopImageButton : UIView


/**图片 imageView*/
@property(nonatomic,strong)UIImageView * imageView;

/**文字 Label*/
@property(nonatomic,strong)UILabel * textLabel;

/**图片*/
@property(nonatomic,strong)id image;
/**文字*/
@property(nonatomic,copy)NSString * name;

-(id)initWithFrame:(CGRect)frame image:(id)image name:(NSString*)name;
@end


@implementation SlideTopImageButton

-(id)initWithFrame:(CGRect)frame image:(id)image name:(NSString *)name{
    if(self = [super initWithFrame:frame]){
        self.image = image;
        self.name = name;
        
        self.backgroundColor = [UIColor clearColor];
        UIImageView * imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        self.imageView = imageView;
        
        UILabel * textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        textLabel.font = [UIFont systemFontOfSize:ISBigScreen?12:10];
        [self addSubview:textLabel];
        self.textLabel = textLabel;
        self.textLabel.text = self.name;
        
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    if([self.image isKindOfClass:[UIImage class]]){
        self.imageView.image = self.image;
    }
    else if([self.image isKindOfClass:[NSString class]]){
        if([[self.image uppercaseString] containsString:@"HTTP"]){
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.image] placeholderImage:nil];
        }else{
            self.imageView.image = [UIImage imageNamed:self.image];
        }
    }
    self.imageView.size = CGSizeMake(self.width*0.25, self.height*0.75);
    
    self.imageView.centerX = self.width * 0.5;
    
    self.textLabel.size = [self.textLabel.text sizeInAre:MaxSize font:self.textLabel.font];
    
    self.textLabel.y = self.imageView.botton+Ratio_home_slide_TopImageCell_space * ScreenHeight;
    self.textLabel.centerX = self.imageView.centerX;
}
@end


@implementation SlideTopImageCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.backgroundColor = [UIColor clearColor];
        self.cellHeight = ScreenHeight * Ratio_home_slide_TopImageCellHeight + 2*globalMargin;
    }
    return self;
}

-(void)setImages:(NSArray *)images names:(NSArray *)names{
    
    for (UIView * v in self.contentView.subviews) {
        [v removeFromSuperview];
    }
    CGFloat W = ScreenWidth * Ratio_leftVC/4.0;
    CGFloat H = self.cellHeight-2*globalMargin;
    for (int i = 0; i<names.count; i++) {
        id image = (!images||images.count==0)?nil:images[i];
        NSString * name = names[i];
        SlideTopImageButton * topImagebutton = [[SlideTopImageButton alloc] initWithFrame:CGRectMake(i*W,globalMargin, W, H) image:image name:name];
        [topImagebutton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonClick:)]];
        topImagebutton.tag = i;
        [self.contentView addSubview:topImagebutton];
    }
}

-(void)buttonClick:(UITapGestureRecognizer*)gesture{
    SlideTopImageButton * btn = (SlideTopImageButton*)gesture.view;
    CategoryViewController * categoryVC = [[CategoryViewController alloc] initWithCat_id:[self.cates[btn.tag] cat_id]];
    [[self getCurrentVC].navigationController pushViewController:categoryVC animated:YES];
}
@end























